package cn.wolfcode.shop.api.page;

import lombok.Getter;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.util.Collections;
import java.util.List;

@Getter  //获得从页面传来的数据
@NoArgsConstructor   //com.alibaba.com.caucho.hessian.io.HessianProtocolException: 'cn.wolfcode.shop.api.page.PageResult' could not be instantiated
public class PageResult implements Serializable {

    private int currentPage = 1;
    private int pageSize = 5;

    private int totalCount;
    private List<?> list;

    private int nextPage;
    private int prevPage;
    private int totalPage;
  //totalCount==0
  public static PageResult empty(int currentPage, int pageSize){
      return new PageResult(0, Collections.EMPTY_LIST, currentPage, pageSize);
  }

    //totalCount!=0
    public PageResult(int totalCount,List<?> list, int currentPage, int pageSize) {

        this.currentPage = currentPage;
        this.pageSize = pageSize;
        this.totalCount = totalCount;
        this.list = list;

        this.totalPage = totalCount % pageSize == 0 ? totalCount / pageSize :totalCount / pageSize +1   ;
        this.prevPage = currentPage - 1 > 0 ? currentPage-1 : 1;
        this.nextPage = currentPage + 1 > totalPage ? totalPage: currentPage + 1 ;
    }

}
